// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_stanceindicator.sqf"
#include "x_setup.sqf"

#define __green [0.4, 0.6745, 0.2784, 1]
#define __yellow [1, 1, 0, 1]
#define __orange [0.8, 0.4, 0, 1]
#define __red [1, 0, 0, 1]
#define __white [1, 1, 1, 1]
#define CTRL(idc) ((uiNamespace getVariable QGVAR(stanceIndicator)) displayCtrl idc)

private ["_pos", "_turned_on", "_prev_ctrls", "_oldspeed", "_old_anims", "_color"];

waitUntil {!isNil QGVAR(still_in_intro)};
waitUntil {!GVAR(still_in_intro)};
sleep 7;

_turned_on = false;
_oldspeed = -1;
_prev_ctrls = [];
_old_anims = "";
_color = __white;

//TODO Make one frame ?

while {true} do {
	if (alive player && {!__pGetVar(xr_pluncon)} && {!GVAR(player_in_vec)} && {!visibleMap} && {isNil "KEGs_SPECTATINGON"} && {!surfaceIsWater (getPosASL player)}) then {
		if (!_turned_on) then {
			12123 cutRsc [QGVAR(stanceIndicator),"PLAIN"];
			_turned_on = true;
		};
		_anims = animationState player;
		_stance = stance player;
		_speed = speed player;
		if (_old_anims != _anims) then {
			_pos = [_anims] call FUNC(animEndsWith);
			_old_anims = _anims;
		};
		if (_oldspeed != _speed) then {
			_colorb = switch(true) do {
				case (_speed <= 2): {__green};
				case (_speed < 13): {__yellow};
				case (_speed < 20): {__orange};
				case (_speed >= 20): {__red};
				default {__green};
			};
			CTRL(2011) ctrlSetBackgroundColor _colorb;
			_oldspeed = _speed;
		};
		
		{
			CTRL(_x) ctrlSetBackgroundColor [1,1,1,0.3];
		} forEach _prev_ctrls;
		_prev_ctrls = [];

		switch (_stance) do {
			case "STAND": {
				if (_pos == "left") then {
					CTRL(2001) ctrlSetBackgroundColor _color;
					CTRL(2009) ctrlSetBackgroundColor _color;
					_prev_ctrls = [2001, 2009];
				} else {
					if (_pos == "right") then {
						CTRL(2001) ctrlSetBackgroundColor _color;
						CTRL(2010) ctrlSetBackgroundColor _color;
						_prev_ctrls = [2001, 2010];
					} else {
						if (_pos == "up") then {
							CTRL(2000) ctrlSetBackgroundColor _color;
							_prev_ctrls = [2000];
						} else {
							if (_pos == "down") then {
								CTRL(2002) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2002];
							} else {
								CTRL(2001) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2001];
							};
						};
					};
				};
			};
			case "CROUCH": {
				if (_pos == "left") then {
					CTRL(2004) ctrlSetBackgroundColor _color;
					CTRL(2009) ctrlSetBackgroundColor _color;
					_prev_ctrls = [2004, 2009];
				} else {
					if (_pos == "right") then {
						CTRL(2004) ctrlSetBackgroundColor _color;
						CTRL(2010) ctrlSetBackgroundColor _color;
						_prev_ctrls = [2004, 2010];
					} else {
						if (_pos == "up") then {
							CTRL(2003) ctrlSetBackgroundColor _color;
							_prev_ctrls = [2003];
						} else {
							if (_pos == "down") then {
								CTRL(2005) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2005];
							} else {
								CTRL(2004) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2004];
							};
						};
					};
				};
			};
			case "PRONE": {
				if (_pos == "left") then {
					CTRL(2007) ctrlSetBackgroundColor _color;
					CTRL(2009) ctrlSetBackgroundColor _color;
					_prev_ctrls = [2007, 2009];
				} else {
					if (_pos == "right") then {
						CTRL(2007) ctrlSetBackgroundColor _color;
						CTRL(2010) ctrlSetBackgroundColor _color;
						_prev_ctrls = [2007, 2010];
					} else {
						if (_pos == "up") then {
							CTRL(2006) ctrlSetBackgroundColor _color;
							_prev_ctrls = [2006];
						} else {
							if (_pos == "down") then {
								CTRL(2008) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2008];
							} else {
								CTRL(2007) ctrlSetBackgroundColor _color;
								_prev_ctrls = [2007];
							};
						};
					};
				};
			};
		};
	} else {
		if (_turned_on) then {
			_turned_on = false;
			12123 cutRsc ["XDEmpty","PLAIN"];
			_oldspeed = -1;
			_old_anims = "";
		};
		waitUntil {sleep 0.221; alive player && {!__pGetVar(xr_pluncon)} && {!GVAR(player_in_vec)} && {!visibleMap} && {isNil "KEGs_SPECTATINGON"} && {!surfaceIsWater (getPosASL player)}};
	};
	sleep 0.4;
};